const QQMapWX = require('./qqmap-wx-jssdk.min.js');

/**
 * 获取用户位置授权，并引导授权，返回结果
 */
export function pointAddresAuth() {
	var that = this;
	return new Promise(resolve => {
		//#ifdef H5
		resolve(true)
		//#endif
		//#ifndef H5
		uni.getSetting({
			success(res) {
				console.log(res)
				let authRes = res.authSetting["scope.userLocation"];
				if (authRes == undefined) {
					uni.authorize({
						scope: "scope.userLocation",
						success(res) {
							console.log('授权结果', res)
							uni.showToast({
								title: '授权成功',
								icon: 'success',
								duration: 1500
							})
							resolve(true)
						},
						fail(res) {
							console.log('授权结果', res)
							uni.showToast({
								title: '授权失败',
								icon: 'none',
								duration: 1500
							})
							resolve(false)
						},
						complete(res) {
							console.log('授权结果', res)
						}
					})

				} else if (!authRes) {
					uni.showModal({
						title: '提示',
						content: '需要获取用户位置授权信息',
						showCancel: true,
						success(res) {
							console.log(res)
							if (res.confirm) {
								uni.openSetting({
									success(res) {
										console.log(res)
										if (res.authSetting["scope.userLocation"]) {
											uni.showToast({
												title: '授权成功',
												icon: 'success',
												duration: 1500
											})
											resolve(true)
										} else {
											uni.showToast({
												title: '授权失败',
												icon: 'none',
												duration: 1500
											})
											resolve(false)
										}
									}
								})
							} else {
								uni.showToast({
									title: '取消授权',
									icon: 'none',
									duration: 1500
								})
								resolve(false)
							}
						}
					})
				} else {
					resolve(true)
				}
			}
		})
		//#endif
	})
}

/**
 * 获取用户当前位置
 */
export function getLoacation() {
	return new Promise(resolve => {
		uni.getLocation({
			type: "wgs84",
			success(res) {
					console.log("res",res)
				var qqmapsdk = new QQMapWX({
					key: 'TJ3BZ-ULQWU-OL7VH-2SJID-X24NO-KFFTF'
				});
				// console.log('位置测试')
				// console.log(qqmapsdk)
				qqmapsdk.reverseGeocoder({
					location: {
						latitude: res.latitude,
						// latitude: 27.0875,
						longitude: res.longitude
						// longitude: 114.904
					},
					success: function(rest) {
						console.log('定位信息')
						console.log(rest)
						uni.setStorageSync("location", rest)
						uni.setStorageSync("latitude", res.latitude)
						uni.setStorageSync("longitude", res.longitude)
						resolve(rest)
					}
				});
			},
			fail(rest){
				console.log("resy",rest)
			}
		})
	})
	
}

/**
 * 选择位置回调
 */
export function regionChoose() {
	var that = this;
	return new Promise(resolve => {
		uni.chooseLocation({
			success(res) {
				var qqmapsdk = new QQMapWX({
					key: 'TJ3BZ-ULQWU-OL7VH-2SJID-X24NO-KFFTF'
				});
				qqmapsdk.reverseGeocoder({
					location: {
						latitude: res.latitude,
						// latitude: 27.0875,
						longitude: res.longitude
						// longitude: 114.904
					},
					success: function(rest) {
						console.log('定位信息')
						console.log(rest)
						resolve(rest)
					},
					fail: function () {
						uni.showToast({
							title: "获取位置信息失败",
							icon: 'error'
						})
					}
				});
		
			}
		})
	})
	
}
